<?php

include_once($cfgPaths['lib'].'cms/type/cmsEditType.o.inc.php');

class cmsEditAppType extends cmsEditType {

 function registerFields () {
   $map[] = $this->name.'appname';
   $map[] = $this->name.'paramstr';
   $this->map = &$map;
 }

 function render_html () {
    global $cfgPaths,$cfgDeveloperMode,$env;
    $appname = $this->getValue($this->name.'appname');
    $html  = '<div class="xicstypeplugin">'."\n";
    $html .= '    <h3>'.$this->descr.':</h3>'.$this->showFields()."\n";
    $html .= '  <table border="0">'."\n";
    $html .= '     <tr>'."\n";
    $html .= '      <td>Parameter:</td>'."\n";
    $html .= '      <td>'."\n";
    $html .= '        <input tabindex="'.$this->sender->getNextTabindex($this->name.'paramstr').'" name="'.$this->name.'paramstr" value="'.htmlspecialchars($this->getValue($this->name.'paramstr')).'" size="50" maxlength="255"><td>'."\n";
    $html .= '     </tr>'."\n";
    $html .= '     <tr>'."\n";
    $html .= '      <td>Applikation:</td>'."\n";
    $html .= '      <td>'."\n";
    $html .= '<select name="'.$this->name.'appname" tabindex="'.$this->sender->getNextTabindex($this->name.'appname').'">'."\n";
    if ($cfgDeveloperMode)
    {
        $dir = $cfgPaths['template'].'cms/app/';
        if (file_exists($dir)){
            $dh = opendir($dir);
            rewinddir($dh);
            while($file = readdir($dh))
            {
                if (!is_dir($dir.$file)){
                    $html .= '<option';
                    if ($file==$appname)
                        $html .= " selected";
                    $html .= '>'.$file.'</option>';
                }
            }
            closedir($dh);
        }
        $dh = opendir($cfgPaths['mod']);
        rewinddir($dh);
        while($file = readdir($dh))
        {
            $modules[] = $file;
        }    
        closedir($dh);
        if (is_array($modules)){
            foreach ($modules As $module)
            {
                if (file_exists($cfgPaths['mod'].$module.'/app')){
                    $dh = opendir($cfgPaths['mod'].$module.'/app');
                    rewinddir($dh);
                    while($file = readdir($dh))
                    {
                        if (!is_dir($cfgPaths['mod'].$module.'/app/'.$file)){
                            $html .= '<option';
                            if ($module.'::'.$file==$appname)
                                $html .= " selected";
                            $html .= '>'.$module.'::'.$file.'</option>';
                        }
                    }
                    closedir($dh);
                }
            }
        }
        $ctx = base_ApplicationContext::getInstance();
        $dh = opendir($ctx->cfgPath('basePath').'modules');
        rewinddir($dh);
        while($file = readdir($dh))
        {
            $modules[] = $file;
        }
        closedir($dh);
        if (is_array($modules)){
            foreach ($modules As $module)
            {
                if (file_exists($ctx->cfgPath('basePath').'modules/'.$module.'/apps')){
                    $dh = opendir($ctx->cfgPath('basePath').'modules/'.$module.'/apps');
                    rewinddir($dh);
                    while($file = readdir($dh))
                    {
                        if (!is_dir($ctx->cfgPath('basePath').'modules/'.$module.'/apps/'.$file)){
                            $html .= '<option';
                            if ('base_'.$module.'::'.$file==$appname)
                                $html .= " selected";
                            $html .= '>base_'.$module.'::'.$file.'</option>';
                        }
                    }
                    closedir($dh);
                }
            }
        }
    }
    else
    {
        $els = $GLOBALS['tdba']->getBySiteAndType($env->getSession('s_currentSite'),'app');
        for ($i=0;$i<count($els);$i++)
        {
            $html .= '<option';
            if ($appname==$els[$i]['filename']) 
                $html .= " selected";
            $html .= ' value="'.$els[$i]['filename'].'">'.$els[$i]['name'].'</option>';
        }
    }
    $html .= '</select></td>'."\n";
    $html .= '     </tr>'."\n";
    $html .= '  </table>'."\n";
    $html  .= '</div><!-- / type selection -->'."\n";
    return $html;
 }

 function render_javascript () {
 }

}


?>